<template>
  <div class="Sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread></SubBread>
      <!-- 筛选器 -->
      <SubFilter @filter-change="filterChange"></SubFilter>
      <!-- 商品面板（排序-商品） -->
      <div class="goods-list">
        <SubSort @sort-change="sortChange"></SubSort>
        <ul>
          <li v-for="goods in goodList" :key="goods.id">
            <GoodsItem :goods="goods" />
          </li>
        </ul>
        <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getData"></XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from './components/sub-category.vue'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import { ref, watch } from 'vue'
import { findSubCategoryGoods } from '@/api/category'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup () {
    const loading = ref(false)
    const finished = ref(false)
    const goodList = ref([])
    const route = useRoute()
    let reqParams = {
      page: 1,
      pageSize: 20
    }
    const getData = () => {
      loading.value = true
      // 发请求携带参数
      reqParams.categoryId = route.params.id
      findSubCategoryGoods(reqParams).then(({ result }) => {
        // 检测数据是否获取成功
        if (result.items.length) {
          // 数据获取成功 追加数据
          goodList.value.push(...result.items)

          // 把page改为下一页页码
          reqParams.page++
        } else {
          // 没有数据 代表加载完成
          finished.value = true
        }
        loading.value = false
      })
    }
    // 更改二级分类的时候，重新加载分类
    watch(() => route.params.id, (newVal) => {
      if (newVal && `/category/sub/${newVal}` === route.path) {
        finished.value = false
        // 导致页表置空，触发XtxInfiniteLoading中加载事件，调用getData事件，重新加载数据
        goodList.value = []
        reqParams = {
          page: 1,
          pageSize: 20
        }
      }
    })

    // 更改排序组件的筛选数据，重新请求
    const sortChange = (sortParams) => {
      // console.log(sortParams)
      finished.value = false
      // 合并请求参数，保留之前参数
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      goodList.value = []
    }

    const filterChange = (getFilterParams) => {
      // console.log(getFilterParams)
      finished.value = false
      reqParams = { ...reqParams, ...getFilterParams }
      reqParams.page = 1
      goodList.value = []
      // 请求已发送
      console.log('请求已经发送', reqParams)
    }

    return { getData, loading, finished, goodList, sortChange, filterChange }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
